package com.wang.userscenter.users.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wang.userscenter.common.util.ConstatFinalUtil;
import com.wang.userscenter.common.util.EncryptUtil;
import com.wang.userscenter.common.util.PageInfoUtil;
import com.wang.userscenter.users.dao.AadminsMapper;
import com.wang.userscenter.users.dao.AroleMapper;
import com.wang.userscenter.users.pojo.Aadmins;
import com.wang.userscenter.users.pojo.Arole;
import com.wang.userscenter.users.service.UsersDbService;

/**
 * 用户Service的实现类
 * <bean id="usersDbServiceImpl" class="">
 */
@Service
public class UsersDbServiceImpl implements UsersDbService{
	/**
	 * 持有dao的引用
	 */
	@Autowired
	private AroleMapper roleMapper;
	@Autowired
	private AadminsMapper adminsMapper;
	@Autowired
	private EncryptUtil encryptUtil;
	
	@Override
	public List<Arole> selectRoleList(PageInfoUtil pageInfoUtil,Map<String,Object> condMap) {
		if (pageInfoUtil != null) {
			Page<Arole> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<Arole> dataList = this.roleMapper.selectList(condMap);
			/* 获取总条数 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			return dataList;
		}
		return this.roleMapper.selectList(condMap);
	}

	@Override
	public Arole selectRoleOne(Map<String,Object> condMap) {
		return this.roleMapper.selectOne(condMap);
	}

	@Override
	public JSONObject saveRoleOne(Arole pojo) {
		/* 拼装json字符串 */
		JSONObject resultJson = new JSONObject();
		
		int effect = this.roleMapper.saveOne(pojo);
		if (effect > 0 ) {
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_SUCCESS);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_SUCCESS));
			/* 对象 */
			JSONObject dataJson = new JSONObject();
			dataJson.put(ConstatFinalUtil.INFO_STR_EFFECT, effect);
			dataJson.put(ConstatFinalUtil.INFO_STR_ID, pojo.getId());
			resultJson.put(ConstatFinalUtil.INFO_STR_DATA, dataJson);
		} else {
			/* 失败 */
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_FAILED);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_FAILED));
		}
		return resultJson;
	}

	@Override
	public JSONObject updateRoleOne(Arole pojo) {
		/* 拼装json字符串 */
		JSONObject resultJson = new JSONObject();
		
		int effect =  this.roleMapper.updateOne(pojo);
		if (effect > 0 ) {
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_SUCCESS);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_SUCCESS));
			/* 对象 */
			JSONObject dataJson = new JSONObject();
			dataJson.put(ConstatFinalUtil.INFO_STR_EFFECT, effect);
			dataJson.put(ConstatFinalUtil.INFO_STR_ID, pojo.getId());
			resultJson.put(ConstatFinalUtil.INFO_STR_DATA, dataJson);
		} else {
			/* 失败 */
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_FAILED);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_FAILED));
		}
		return resultJson;
		
	}

	@Override
	public JSONObject deleteRoleOne(Map<String,Object> condMap) {
		/* 拼装json字符串 */
		JSONObject resultJson = new JSONObject();
		
		int effect = this.roleMapper.deleteOne(condMap);
		if (effect > 0 ) {
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_SUCCESS);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_SUCCESS));
			/* 对象 */
			JSONObject dataJson = new JSONObject();
			dataJson.put(ConstatFinalUtil.INFO_STR_EFFECT, effect);
			resultJson.put(ConstatFinalUtil.INFO_STR_DATA, dataJson);
		} else {
			/* 失败 */
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_FAILED);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_FAILED));
		}
		return resultJson;
	}
	
	@Override
	public List<Aadmins> selectAdminsList(PageInfoUtil pageInfoUtil,Map<String,Object> condMap) {
		List<Aadmins> resuList = new ArrayList<Aadmins>();
		if (pageInfoUtil != null) {
			Page<Arole> page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			resuList = this.adminsMapper.selectList(condMap);
			/* 获取总条数 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
		} else {
			resuList = this.adminsMapper.selectList(condMap);
		}
		for (Aadmins admins : resuList) {
			/* 拿着管理员表中的roleId到角色表中查询id与roleId一样的数据 */
			int roleId = admins.getRoleId();
			
			Map<String,Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("id", roleId);
			Arole role = this.selectRoleOne(paramsMap);
			admins.setRole(role);
		}
		return resuList;
	}

	@Override
	public Aadmins selectAdminsOne(Map<String,Object> condMap) {
		Aadmins admins = this.adminsMapper.selectOne(condMap);
		if (admins != null) {
			/* 拿着管理员表中的roleId到角色表中查询id与roleId一样的数据 */
			int roleId = admins.getRoleId();
			
			Map<String,Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("id", roleId);
			Arole role = this.selectRoleOne(paramsMap);
			admins.setRole(role);
		}
		return admins;
	}

	@Override
	public JSONObject saveAdminsOne(Aadmins pojo) throws Exception {
		/* 拼装json字符串 */
		JSONObject resultJson = new JSONObject();
		/* 先根据邮箱查询,是否存在 */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("email", pojo.getEmail());
		Aadmins adminsRes =  this.selectAdminsOne(condMap);
		if (adminsRes != null) {
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_EMAIL_EXISTS);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_EMAIL_EXISTS));
			/* 根据邮箱已经查询到了 */
			return resultJson;
		}
		
		/* 有话说,有事做 */
		String encodeStr = this.encryptUtil.encrypt(pojo.getPassword());
		pojo.setPassword(encodeStr);
		pojo.setCreateTime(new Date());
		pojo.setUpdateTime(new Date());
		
		int effect = this.adminsMapper.saveOne(pojo);
		if (effect > 0 ) {
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_SUCCESS);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_SUCCESS));
			/* 对象 */
			JSONObject dataJson = new JSONObject();
			dataJson.put(ConstatFinalUtil.INFO_STR_EFFECT, effect);
			dataJson.put(ConstatFinalUtil.INFO_STR_ID, pojo.getId());
			resultJson.put(ConstatFinalUtil.INFO_STR_DATA, dataJson);
		} else {
			/* 失败 */
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_FAILED);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_FAILED));
		}
		
//		String str = null;
//		str.toString();
		if ("1".equalsIgnoreCase("1")) {
			throw new Exception();
		}
		return resultJson;
	}

	@Override
	public JSONObject updateAdminsOne(Aadmins pojo) {
		/* 拼装json字符串 */
		JSONObject resultJson = new JSONObject();
		
		/* 先根据邮箱查询,是否存在 */
		Map<String,Object> condMap = new HashMap<String, Object>();
		condMap.put("email", pojo.getEmail());
		Aadmins adminsRes =  this.selectAdminsOne(condMap);
		if (adminsRes != null && !adminsRes.getId().equals(pojo.getId())) {
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_EMAIL_EXISTS);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_EMAIL_EXISTS));
			/* 根据邮箱已经查询到了 */
			return resultJson;
		}
		
		/* 有话说,有事做 */
		if (this.encryptUtil.checkStr(pojo.getPassword()) ) {
			String encodeStr = this.encryptUtil.encrypt(pojo.getPassword());
			pojo.setPassword(encodeStr);
		}
		/* 更新时间 */
		pojo.setUpdateTime(new Date());
		
		int effect =  this.adminsMapper.updateOne(pojo);
		if (effect > 0 ) {
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_SUCCESS);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_SUCCESS));
			/* 对象 */
			JSONObject dataJson = new JSONObject();
			dataJson.put(ConstatFinalUtil.INFO_STR_EFFECT, effect);
			dataJson.put(ConstatFinalUtil.INFO_STR_ID, pojo.getId());
			resultJson.put(ConstatFinalUtil.INFO_STR_DATA, dataJson);
		} else {
			/* 失败 */
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_FAILED);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_FAILED));
		}
		return resultJson;
		
	}

	@Override
	public JSONObject deleteAdminsOne(Map<String,Object> condMap) {
		/* 拼装json字符串 */
		JSONObject resultJson = new JSONObject();
		
		int effect = this.adminsMapper.deleteOne(condMap);
		if (effect > 0 ) {
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_SUCCESS);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_SUCCESS));
			/* 对象 */
			JSONObject dataJson = new JSONObject();
			dataJson.put(ConstatFinalUtil.INFO_STR_EFFECT, effect);
			resultJson.put(ConstatFinalUtil.INFO_STR_DATA, dataJson);
		} else {
			/* 失败 */
			resultJson.put(ConstatFinalUtil.INFO_STR_CODE, ConstatFinalUtil.INFO_STR_FAILED);
			resultJson.put(ConstatFinalUtil.INFO_STR_INFO, ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_STR_FAILED));
		}
		return resultJson;
	}
}